package m1_simple;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("www.wht6.cn");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection connection = f.newConnection();
        Channel channel = connection.createChannel();

        //2.定义队列 helloworld
        channel.queueDeclare("helloworld", false, false, false, null);

        //创建回调对象
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                byte[] body = delivery.getBody();
                String msg = new String(body);
                System.out.println("收到:"+msg);
            }
        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {}
        };

        //3.消费,接收消息
        channel.basicConsume("helloworld", true,deliverCallback,cancelCallback);
    }
}
